<!--
  * 变化明细
  *
-->
<template>
  <!---------- 查询表单form begin ----------->
  <a-form :model="from" labelAlign="left" class="smart-margin-bottom10">
    <a-row :gutter="[8, 10]">
      <a-col :span="{ sm: 7, md: 6 }">
        <a-form-item label="单据日期" class="smart-margin-bottom0">
          <custom-range-picker v-model:value="from.create_time" />
        </a-form-item>
      </a-col>
      <a-col :span="4">
        <a-form-item label="单据编号" class="smart-margin-bottom0">
          <a-input v-model:value.trim="from.order_no" placeholder="单据编号" allow-clear />
        </a-form-item>
      </a-col>
      <a-col :span="4">
        <a-form-item label="类别" class="smart-margin-bottom0">
          <a-select ref="select" v-model:value="from.type" placeholder="类别" allowClear>
            <a-select-option :value="1">入库</a-select-option>
            <a-select-option :value="2">出库</a-select-option>
          </a-select>
        </a-form-item>
      </a-col>
      <a-col :span="4">
        <a-form-item label="类型" class="smart-margin-bottom0">
          <a-select ref="select" v-model:value="from.log_type" placeholder="类型" allowClear>
            <a-select-option :value="1">采购入库</a-select-option>
            <a-select-option :value="2">调拨入库</a-select-option>
            <a-select-option :value="3">司机回库</a-select-option>
            <a-select-option :value="4">异常回库</a-select-option>
            <a-select-option :value="5">换货回库</a-select-option>
            <a-select-option :value="6">采退出库</a-select-option>
            <a-select-option :value="7">访销出库</a-select-option>
            <a-select-option :value="8">调拨出库</a-select-option>
            <a-select-option :value="9">换货出库</a-select-option>
            <a-select-option :value="10">装车入库</a-select-option>
            <a-select-option :value="11">签收出库</a-select-option>
            <a-select-option :value="12">签收入库</a-select-option>
            <a-select-option :value="13">回退出库</a-select-option>
            <a-select-option :value="14">回退入库(移动仓)</a-select-option>
            <a-select-option :value="15">回退入库(固定仓)</a-select-option>
            <a-select-option :value="16">移货出库(固定仓)</a-select-option>
            <a-select-option :value="17">移货入库(固定仓)</a-select-option>
            <a-select-option :value="18">订货出库</a-select-option>
            <a-select-option :value="19">仓库货物移位(入库)</a-select-option>
            <a-select-option :value="20">仓库货物移位(出库)</a-select-option>
            <a-select-option :value="21">司机回库单回库</a-select-option>
          </a-select>
        </a-form-item>
      </a-col>
      <a-col :span="4">
        <a-button-group>
          <a-button type="primary" html-type="submit" @click="onSearch">
            <template #icon>
              <SearchOutlined />
            </template>
            查询
          </a-button>
          <a-button @click="resetQueryData">
            <template #icon>
              <ReloadOutlined />
            </template>
            重置
          </a-button>
        </a-button-group>
      </a-col>
    </a-row>
  </a-form>
  <a-table :dataSource="tableData" :columns="columns" :pagination="false" rowKey="id" :scroll="{ x: 'max-content', y: 427 }" size="small" bordered :showExpandColumn="false" expandRowByClick>
    <template #bodyCell="{ text, record, column }">
      <template v-if="column.dataIndex === 'type'"> {{ PROGRESS_TYPE_ENUM[text] }} </template>
      <template v-if="column.dataIndex === 'log_type'"> {{ PROGRESS_LOG_ENUM[text] }} </template>
      <template v-if="column.dataIndex === 'attribute_type'">{{ record?.warehouse?.name }}({{ getDescByValue('WAREHOUSE_ATTRIBUTE_TYPE', record?.warehouse?.attribute_type) }})</template>
      <template v-if="column.dataIndex === 'order_no'">
        <a @click="toSaleOrder(record)">{{ text }}</a>
      </template>
    </template>
  </a-table>
  <div class="smart-query-table-page">
    <a-pagination showSizeChanger showQuickJumper show-less-items :pageSizeOptions="PAGE_SIZE_OPTIONS" :defaultPageSize="queryForm.limit" v-model:current="queryForm.page" v-model:pageSize="queryForm.limit" :total="total" @change="queryData" @showSizeChange="queryData" :show-total="(total) => `共${total}条`" />
  </div>
</template>
<script setup lang="ts">
import CustomRangePicker from '@/components/business/custom-range-picker/index.vue'
import _ from 'lodash'
import { useQuery } from '@/hooks/web/use-query-hook'
import { inventoryApi } from '@/api/business/storage/warehouse-api'
import { PAGE_SIZE_OPTIONS, DEFAULT_TIME_RANGE } from '@/constants/system/common-const'
import { useEnums } from '@/hooks/plugins/useEnums'
const { getDescByValue } = useEnums()
const props = defineProps({
  data: {
    type: Object,
    default: () => ({})
  }
})
const emit = defineEmits(['reload'])

const enum PROGRESS_TYPE_ENUM {
  '入库' = 1,
  '出库'
}

const enum PROGRESS_LOG_ENUM {
  '采购入库' = 1,
  '调拨入库',
  '司机回库',
  '异常回库',
  '换货回库',
  '采退出库',
  '访销出库',
  '调拨出库',
  '换货出库',
  '装车入库',
  '签收出库',
  '签收入库',
  '回退出库',
  '回退入库(移动仓)',
  '回退入库(固定仓)',
  '移货出库(固定仓)',
  '移货入库(固定仓)',
  '订货出库',
  '仓库货物移位(入库)',
  '仓库货物移位(出库)',
  '司机回库单回库'
}

const from = ref({
  create_time: DEFAULT_TIME_RANGE.value,
  type: null,
  log_type: null,
  order_no: ''
})

// 表格
const columns = reactive([
  {
    title: '日期时间',
    dataIndex: 'create_time'
  },
  {
    title: '类别',
    dataIndex: 'type'
  },
  {
    title: '类型',
    dataIndex: 'log_type'
  },
  {
    title: '出入库',
    dataIndex: 'attribute_type'
  },
  {
    title: '商品数量',
    dataIndex: 'stock'
  },
  {
    title: '货值金额',
    dataIndex: 'goods_amount'
  },
  {
    title: '单据编号',
    dataIndex: 'order_no'
  }
])

const { queryForm, tableData, tableLoading, total, resetQuery, onSearch, queryData, deleteData } = useQuery({
  fetchDataApi: async (params) => {
    console.log(props.data)
    const data = {
      ...params,
      ...from.value,
      goods_id: props.data.id,
      warehouse_id: props.data.warehouse_id
    }
    const res = await inventoryApi.batchLog(data)
    return {
      list: res.data.data,
      total: res.data.total
    }
  },
  queryFormFilter: {},
  queryFormOp: {}
})

function resetQueryData() {
  from.value.create_time = DEFAULT_TIME_RANGE.value
  from.value.type = null
  from.value.log_type = null
  from.value.order_no = ''
  resetQuery()
}

watch(
  () => props.data,
  (e) => {
    if (e) {
      from.value.create_time = DEFAULT_TIME_RANGE.value
      from.value.type = null
      from.value.log_type = null
      from.value.order_no = ''
      queryData()
    }
  },
  { immediate: true }
)

// 跳转至退货单
const router = useRouter()
function toSaleOrder(record) {
  if (record.related_order_type == 'order') {
    router.push(`/sale/sales?no=${record.order_no}`)
  } else if (record.related_order_type == 'return') {
    router.push(`/sale/return?no=${record.order_no}`)
  } else if (record.related_order_type == 'exchange') {
    router.push(`/sale/barter?no=${record.order_no}`)
  } else if (record.related_order_type == 'book') {
    router.push(`/sale/goods?no=${record.order_no}`)
  } else if (record.related_order_type == 'return_book') {
    router.push(`/sale/refund?no=${record.order_no}`)
  } else if (record.related_order_type == 'purchase') {
    router.push(`/procurement/list?no=${record.order_no}`)
  } else if (record.related_order_type == 'purchase_return') {
    router.push(`/procurement/return-list?no=${record.order_no}`)
  } else if (record.related_order_type == 'transfer') {
    router.push(`/storage/inwarehouse/transfer?no=${record.order_no}`)
  } else if (record.related_order_type == 'moving') {
    router.push(`/storage/inwarehouse/shift?no=${record.order_no}`)
  } else if (record.related_order_type == 'process') {
    router.push(`/storage/inwarehouse/processing?no=${record.order_no}`)
  } else if (record.related_order_type == 'driverreturnorder') {
    router.push(`/storage/store/return-warehouse?return_no=${record.order_no}`)
  }
}
</script>
